package edu.mis.modules.room;

import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import edu.mis.model.Room;
import edu.mis.model.RoomType;
import edu.mis.modules.role.RoleInterceptor;
import edu.mis.modules.roomType.RoomTypeController;

/**
 * 商品控制器
 *
 */
public class RoomController extends Controller {
	
	public static RoomService service = new RoomService();

    @Clear(RoleInterceptor.class)
	public void index() {
		render("room.html");
	}
    @Clear(RoleInterceptor.class)
	public void list(){
		setAttr("data", service.findAllRoomWithRoomTypeAndStatus());
		renderJson();  
	}

	/**
	 * 添加商品视图
	 */
    @Clear(RoleInterceptor.class)
	public void add() {
		setAttr("roomType", RoomTypeController.service.findAllRoomType());
		    //render("add.html");  //（默认对应add.html，否则需显性指定，如render("addRoom.html")）
	}

	public void addRoom(){
		Room room  =  getModel(Room.class);   //使用getModel方法获取表单值，表单中name应使用对象名.属性名，如room.id, room.roomQty,详见jfinal手册
		if(service.addRoom(room)){
			setAttr("result", true); 
			setAttr("msg", "添加商品成功!"); 
		}else{
			setAttr("result",false);
			setAttr("msg", "添加失败，商品名重复!"); 
		}
		renderJson(); 
	}
	
	/**
	 * 修改商品信息视图（默认对应edit.html，否则需显性指定，如render("editRoomQty.html")，）
	 */
	public void edit() {
		setAttr("room", service.findRoomById(getParaToInt("id")));  //此处room应与表单中${(room.name)!}等中的room保持一致。
	}

    public void updateRoom(){
        Room room  =  getModel(Room.class);
        if(service.updateRoom(room)){
            setAttr("result", true);
            setAttr("msg", "修改商品信息成功!");
        }else{
            setAttr("result", false);
        }
        renderJson();
    }

//	public void updateRoomQty(){
//		int id=this.getParaToInt("id");   //使用getPara获取表单值，与getModel方法获得表单值不同，name不能使用对象名.属性名，如room.roomQty，而直接使用属性名，如roomQty
//		int newRoomQty = this.getParaToInt("roomQty");
//		if(service.updateRoomQty(id, newRoomQty)){
//			setAttr("result", true);
//			setAttr("msg", "修改商品信息成功!");
//		}else{
//			setAttr("result", false);
//		}
//		renderJson();
//	}
	
	/**
	 * 商品分类视图
	 */
    @Clear(RoleInterceptor.class)
	public void view_classify() {
		setAttr("room", service.findRoomById(getParaToInt("id")));  //此处room应与表单中${(room.name)!}等中的room保持一致。
		setAttr("type",RoomType.dao.findAll());//分类前得到所有权限
		render("classify.html");
	}
	
	/**
	 * 商品分类
	 */
	public void classify(){
		int id=this.getParaToInt("id");   
		int typeId = this.getParaToInt("typeId");
		if(service.classifyRoom(id, typeId)){
			setAttr("result", true);
			setAttr("msg", "商品分类成功!");
		}else{
			setAttr("result", false);
			setAttr("msg", "商品分类失败!");
		}
		renderJson();
	}
	/**
	 * 删除商品
	 * 测试访问地址：localhost:8080/room/delete?id=1
	 */
	public void delete() {
		if(service.deleteRoomById(getParaToInt("id"))){
			setAttr("result", true);
			setAttr("msg", "删除商品成功!");
		}else{
			setAttr("result", false);
		}	
		renderJson();
	}
    @Clear(RoleInterceptor.class)
	public void view_checkinList(){
		render("checkinList.html");
	}
    @Clear(RoleInterceptor.class)
	public void checkinList(){
		setAttr("data", service.findRoomWithTypeAndStatus(getParaToInt("roomTypeId"), 2));
		renderJson();
	}

}


